// 本工具方法主要为一些通用打开页面的方法的封装。

/******************************************************************************
 *                                                                             *
 *                   open Html pop-up window                                   *
 *                   data param in order to pass values across pages.          *
 *                   it is a Json Object                                       *
 *                   e.g : {id:123,name:'zhangsan'}                            *
 *                   callback is called on the method is return                *
 *                   it is type must be a function                             *
 *                   when window close the action implicit pass                *
 *                   the method as the first param                             *
 *                                                                             *
 ******************************************************************************/
function commonNoHeadEdit(url, data, width, height, callback) {
    var args = arguments;
    mini.open({
        // targetWindow: window,
        url: url,
        width: width,
        height: height,
        allowDrag: false,
        allowResize: false,
        showCloseButton: false,
        showHeader:false,
        onload: function () {
            if (data != null) {
                var iframe = this.getIFrameEl();
                iframe.contentWindow.SetData(data);
            }
        },
        ondestroy: function (action) {
            if (typeof callback === "function") {
                var options = {};
                for (var i = 6; i < args.length; i++) {
                    options[i - 6] = args[i];
                }
                if (options.length == 0) {
                    callback(action);
                } else {
                    callback(action, options);
                }

            }
        }
    });
}

/******************************************************************************
 *                                                                             *
 *                   open Html pop-up window                                   *
 *                   data param in order to pass values across pages.          *
 *                   it is a Json Object                                       *
 *                   e.g : {id:123,name:'zhangsan'}                            *
 *                   callback is called on the method is return                *
 *                   it is type must be a function                             *
 *                   when window close the action implicit pass                *
 *                   the method as the first param                             *
 *                                                                             *
 ******************************************************************************/
function commonEdit(url, title, data, width, height,allowDrag,allowResize,showCloseButton, callback) {
    var args = arguments;
    mini.open({
        // targetWindow: window,
        url: url,
        title: title,
        width: width,
        height: height,
        allowDrag: allowDrag,
        allowResize: allowResize,
        showCloseButton: showCloseButton,
        onload: function () {
            if (data != null && data != "" ) {
                var iframe = this.getIFrameEl();
                iframe.contentWindow.SetData(data);
            }
        },
        ondestroy: function (action) {
            if (typeof callback === "function") {
                var options = {};
                for (var i = 6; i < args.length; i++) {
                    options[i - 6] = args[i];
                }
                if (options.length == 0) {
                    callback(action);
                } else {
                    callback(action, options);
                }

            }
        }
    });
}

/******************************************************************************
 *                                                                             *
 *                   when need pass values across pages                        *
 *                   the method will complete the assignment                   *
 *                                                                             *
 ******************************************************************************/
function commonSetData(data) {
    if (data.action == "edit") {
        //跨页面传递的数据对象，克隆后才可以安全使用
        data = mini.clone(data);
        form.setData(data);
        form.setChanged(false); //设置是否变动
    }
}

/******************************************************************************
 *                                                                             *
 *                   use element's id get the form object                      *
 *                                                                             *
 ******************************************************************************/
function getForm(form) {
    var form = new mini.Form(form);
    form.validate();
    if (form.isValid() == false) {
        return;
    }
    return form.getData();
}

/******************************************************************************
 *                                                                             *
 *                   common save method                                        *
 *                   when the action is update                                 *
 *                   the param is like {_method:'put',id:123}                  *
 *                   when the action is insert the '_method' is not must       *
 *                                                                             *
 ******************************************************************************/
function commonSaveData(url, data, callback) {
    var args = arguments;
    $.ajax({
        url: url,
        type: "post",
        data: data,
        success: function (success) {
            if(success.code == 4000){
                mini.alert("此表单正在被操作");
            }else if (success.code==1) {
                if (typeof callback === "function") {
                    var options = {};
                    for (var i = 3; i < args.length; i++) {
                        options[i - 3] = args[i];
                    }
                    if (options.length == 0) {
                        callback(success);
                    } else {
                        callback(success, options);
                    }

                }
            } else if(success.code == 2004){
                mini.alert(success.message);
            } else if(success.code == 6000){
                mini.alert(success.message);
            } else if(success.code == 5599){
                mini.alert(success.message);
            } else {
                mini.alert("保存失败");
            }
        }
    });
}

/******************************************************************************
 *                                                                             *
 *                   common remove single row data method                      *
 *                   the data param is like {_method:'delete',id:123}          *
 *                   the row param is the selected row                         *
 *                                                                             *
 ******************************************************************************/
function commonRemove(url, row, data, callback) {
    var args = arguments;
    if (row) {
        mini.confirm("确定删除选中记录？",
            "确定？",
            function (action) {
                if (action == "ok") {
                    $.ajax({
                        url: url,
                        type: "post",
                        data: data,
                        dataType: "json",
                        success: function (text) {
                            if(text.code == 1){
                                mini.alert("删除成功! ");
                            }else if(text.code==4000){
                                mini.alert(text.message);
                            }
                            if (typeof callback === "function") {
                                var options = {};
                                for (var i = 4; i < args.length; i++) {
                                    options[i - 4] = args[i];
                                }
                                if (options.length == 0) {
                                    callback(text);
                                } else {
                                    callback(text, options);
                                }
                            }
                        },
                        error: function () {
                            mini.alert("删除失败! ");
                        }
                    });
                }
            });
    } else {
        mini.alert("请选中一条记录");
    }
}

/******************************************************************************
 *                                                                             *
 *                   common remove single row data method                      *
 *                   the data param is like {_method:'delete',id:123}          *
 *                   the row param is the selected row                         *
 *                                                                             *
 ******************************************************************************/
function commonAssignedRemove(url, row, data, callback) {
    var args = arguments;
    if (confirm("确定取消授权? ")) {
        if (row) {
            $.ajax({
                url: url,
                type: "post",
                data: data,
                dataType: "json",
                success: function (text) {
                    mini.alert("取消成功! ");
                    if (typeof callback === "function") {
                        var options = {};
                        for (var i = 4; i < args.length; i++) {
                            options[i - 4] = args[i];
                        }
                        if (options.length == 0) {
                            callback(text);
                        } else {
                            callback(text, options);
                        }

                    }
                },
                error: function () {
                    mini.alert("取消失败! ");
                }
            });
        } else {
            mini.alert("请选中一条记录! ");
        }
    }
}

/******************************************************************************
 *                                                                             *
 *              the common ajax method                                         *
 *              when the request is success                                    *
 *              the callback method will exec ,if the param                    *
 *              msg is not null the prompt message will show or                *
 *              the default rerurn data's message will show                    *
 *                                                                             *
 ******************************************************************************/
function commonAjax(url, type, dataType, data, msg, callback) {
    var args = arguments;
    $.ajax({
        url: url,
        type: type,
        dataType: dataType,
        data: data,
        success: function (text) {
            var o = mini.decode(text);
            var code=o.code;
            var message=o.message;
            if (code == 1 && msg != undefined && msg != null && msg != "") {
                mini.alert(msg);
            }else if(code == 1&&message != undefined && message != null && message != ""){
                mini.alert(o.message);
            }else if(msg != undefined && msg != null && msg != ""){
                mini.alert(o.message);
                return;
            }
            if (typeof callback === "function") {
                var options = {};
                for (var i = 6; i < args.length; i++) {
                    options[i - 6] = args[i];
                }
                if (options.length == 0) {
                    callback(text);
                } else {
                    callback(text, options);
                }
            }

        },
        error: function (jqXHR, textStatus, errorThrown) {
            mini.alert("请求失败...");
        }
    });
}

/******************************************************
 *                                                    *
 *                    7 通用资源跳转                   *
 *                                                    *
 *****************************************************/
function commonToRes(resid, callback) {
    var args = arguments;
    $.ajax({
        url: path + "/resource?1=1&resid=" + resid,
        type: "get",
        dataType: "json",
        success: function (text) {
            if (typeof callback === "function") {
                var options = {};
                for (var i = 2; i < args.length; i++) {
                    options[i - 2] = args[i];
                }
                if (options.length == 0) {
                    callback(text);
                } else {
                    callback(text, options);
                }
            }
        },
    });
}

/******************************************************
 *                                                    *
 *        8 下拉框(mini-combobox)单表查询                  *
 *                                                    *
 *****************************************************/
function commonComboBoxSearch(url, data, callback) {
    var args = arguments;
    $.ajax({
        url: url,
        type: "get",
        data: data,
        success: function (success) {
                if (typeof callback === "function") {
                    var options = {};
                    for (var i = 3; i < args.length; i++) {
                        options[i - 3] = args[i];
                    }
                    if (options.length == 0) {
                        callback(success);
                    } else {
                        callback(success, options);
                    }
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            mini.alert("没有相应资源");
        }
    });
}